using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Runtime.InteropServices;

namespace SMSSend
{
	/// <summary>
	/// Summary description for WinForm.
	/// </summary>
	public class WinForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private System.Windows.Forms.Button btnSets;
		private System.Windows.Forms.Button btnShowFirstSet;
		private System.Windows.Forms.Button btnSendSMS;
		private System.Windows.Forms.TextBox txtNumber;
		private System.Windows.Forms.TextBox txtMessage;

		private SMSNokia sms;

		public WinForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose (bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnSets = new System.Windows.Forms.Button();
			this.btnShowFirstSet = new System.Windows.Forms.Button();
			this.btnSendSMS = new System.Windows.Forms.Button();
			this.txtNumber = new System.Windows.Forms.TextBox();
			this.txtMessage = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// btnSets
			// 
			this.btnSets.Location = new System.Drawing.Point(32, 16);
			this.btnSets.Name = "btnSets";
			this.btnSets.Size = new System.Drawing.Size(168, 23);
			this.btnSets.TabIndex = 3;
			this.btnSets.Text = "Get number of parameter sets";
			this.btnSets.Click += new System.EventHandler(this.btnSets_Click);
			// 
			// btnShowFirstSet
			// 
			this.btnShowFirstSet.Location = new System.Drawing.Point(32, 48);
			this.btnShowFirstSet.Name = "btnShowFirstSet";
			this.btnShowFirstSet.Size = new System.Drawing.Size(168, 23);
			this.btnShowFirstSet.TabIndex = 4;
			this.btnShowFirstSet.Text = "Show first parameter set";
			this.btnShowFirstSet.Click += new System.EventHandler(this.btnShowFirstSet_Click);
			// 
			// btnSendSMS
			// 
			this.btnSendSMS.Location = new System.Drawing.Point(32, 224);
			this.btnSendSMS.Name = "btnSendSMS";
			this.btnSendSMS.Size = new System.Drawing.Size(168, 23);
			this.btnSendSMS.TabIndex = 2;
			this.btnSendSMS.Text = "Send SMS";
			this.btnSendSMS.Click += new System.EventHandler(this.btnSendSMS_Click);
			// 
			// txtNumber
			// 
			this.txtNumber.Location = new System.Drawing.Point(24, 96);
			this.txtNumber.Name = "txtNumber";
			this.txtNumber.Size = new System.Drawing.Size(248, 20);
			this.txtNumber.TabIndex = 0;
			this.txtNumber.Text = "";
			// 
			// txtMessage
			// 
			this.txtMessage.Location = new System.Drawing.Point(24, 128);
			this.txtMessage.MaxLength = 160;
			this.txtMessage.Multiline = true;
			this.txtMessage.Name = "txtMessage";
			this.txtMessage.Size = new System.Drawing.Size(248, 80);
			this.txtMessage.TabIndex = 1;
			this.txtMessage.Text = "This is a test message.";
			// 
			// WinForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.txtMessage);
			this.Controls.Add(this.txtNumber);
			this.Controls.Add(this.btnSendSMS);
			this.Controls.Add(this.btnShowFirstSet);
			this.Controls.Add(this.btnSets);
			this.Name = "WinForm";
			this.Text = "WinForm";
			this.Load += new System.EventHandler(this.WinForm_Load);
			this.Closed += new System.EventHandler(this.WinForm_Closed);
			this.ResumeLayout(false);
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Run(new WinForm());
		}

		private void ShowCOMException(COMException ex)
		{
			MessageBox.Show("Source: " + ex.Source + Environment.NewLine +
				"Description: " + ex.Message + Environment.NewLine +
				"HelpContext: " + ex.HelpLink + Environment.NewLine,
				"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
		}

		private void btnSets_Click(object sender, System.EventArgs e)
		{
			byte sets;
			try
			{
				sets = sms.GetNumberOfSets();
			}
			catch(COMException ex)
			{
				ShowCOMException(ex);
				return;
			}
			MessageBox.Show(sets.ToString() + " sets.");
		}

		private void btnShowFirstSet_Click(object sender, System.EventArgs e)
		{
			ParameterSet parameters;
			try
			{
				parameters = sms.GetParameterSet(1);
			}
			catch(COMException ex)
			{
				ShowCOMException(ex);
				return;
			}

			string info = "ParameterSetName: " + parameters.ParameterSetName + Environment.NewLine +
				"DataCodingScheme: " + sms.GetDataCodingSchemeText(parameters.DataCodingScheme) + Environment.NewLine +
				"ProtocolID: " + sms.GetProtocolIDText(parameters.ProtocolID) + Environment.NewLine +
				"ValidityPeriodRelative: " + sms.GetRelativeValidityText(parameters.ValidityPeriodRelative) +
					Environment.NewLine +
				"ServiceCenterAddress: " + parameters.ServiceCenterAddress;

			MessageBox.Show(this, info, "Parameter set 1");
		}


		private void btnSendSMS_Click(object sender, System.EventArgs e)
		{
			try
			{
				sms.SendSMS(txtMessage.Text, txtNumber.Text);
			}
			catch(COMException ex)
			{
				ShowCOMException(ex);
			}
		}

		private void WinForm_Load(object sender, System.EventArgs e)
		{
			try
			{
				sms = new SMSNokia();
			}
			catch(COMException ex)
			{
				ShowCOMException(ex);
				this.Close();
			}
		}

		private void WinForm_Closed(object sender, System.EventArgs e)
		{
			sms = null;
		}
	}
}
